package util.database.index;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import core.category.model.Category;
import core.product.model.Product;
import core.product.model.ProductSearchKey;
import core.util.HibernateCoreUtil;
import core.util.database.index.model.ProductSearchIndex;
import dbinit.InitProductSearchIndex;

/*
 * this is singalton class 
 */
public class CreateIndex 
{
	public static void main(String argv[])
	{
		//running product search index
		CreateIndex crtIdx = CreateIndex.getInstance();
		crtIdx.doProductSearchIndex();
	}
	private static CreateIndex createIndex ;
	
	private CreateIndex()
	{
		
	}
	
	public static CreateIndex getInstance()
	{
		if(createIndex == null)
		{
			createIndex = new CreateIndex();
		}
		return createIndex ;
	}
	public void doCategoryIndex()
	{
		System.out.println("Start category search index");
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		Map<Product,Set<String>> prodCateIdx = new HashMap<Product,Set<String>>();
		try 
		{
				transaction = session.beginTransaction();
				Iterator <Category> cateLstItr = session.createQuery("from Category").list().iterator();
				while(cateLstItr.hasNext())
				{
					Category cate = cateLstItr.next();
					Set<Product> prodSet = cate.getProducts();
					if(prodSet.size() > 0)
					{
						Iterator<Product> prodItr = prodSet.iterator();
						while(prodItr.hasNext())
						{
							Product p = prodItr.next();
							if(prodCateIdx.get(p) == null)
							{
								Set<String> cateIdx = new HashSet<String>();
								cateIdx.add(cate.getName());
								prodCateIdx.put(p,cateIdx);
							}
							else
							{
								Set<String> cateIdx = prodCateIdx.get(p);
								cateIdx.add(cate.getName());
								prodCateIdx.put(p,cateIdx);
							}
						}
 					}
				}
				Iterator <Product> prodItr = prodCateIdx.keySet().iterator();
				while(prodItr.hasNext())
				{
					Product p = prodItr.next();
					String newks = new String("");
					Iterator<String> newKes = prodCateIdx.get(p).iterator();
					while(newKes.hasNext())
					{
						newks = newks + " " + newKes.next();
					}
					ProductSearchIndex psk = InitProductSearchIndex.getProductSearchIndexByProduct(p);
					newks = newks + psk.getSearchKey();
					InitProductSearchIndex.update(psk, newks);
				}
				transaction.commit();

		}
		catch (HibernateException e) 
		{
			// TODO: handle exception
			transaction.rollback();
			e.printStackTrace();
		}
		finally 
		{
			session.close();
		}
	}
	
	public void doProductSearchIndex()
	{
		System.out.println("Start search index");
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try 
		{
				transaction = session.beginTransaction();
				Iterator <Product> productLstItr = session.createQuery("from Product").list().iterator();
				while(productLstItr.hasNext())
				{
					Product p = productLstItr.next();
					Set<String> psks = new HashSet<String>();
					psks.add(p.getSku()); 
					Iterator <ProductSearchKey> psk = p.getProductSearchKeys().iterator();
					while(psk.hasNext())
					{
						psks.add(psk.next().getSearchKey());
					}
					String searchKey = new String("");
					Iterator<String> searchks = psks.iterator();
					while(searchks.hasNext())
					{
						searchKey = searchKey + " "+ searchks.next();
					}
					InitProductSearchIndex.doinit(searchKey, p.getUuid());
				}
				transaction.commit();
				doCategoryIndex();
		}
		catch (HibernateException e) 
		{
			// TODO: handle exception
			transaction.rollback();
			e.printStackTrace();
		}
		finally 
		{
			session.close();
		}
		
	}
	
}
